// login. js

const ModuleLogin = {
    state: {
        dialogVisible: false,
        token: [],
        loginShow: false,
        userFlag: false,
        historyFlag: false,
        cartoonFlag: false,
        return: {
        }
    },

    getters: {
    },

    mutations: {
        // 点击显示登录
        loginFun(state) {
            state.dialogVisible = !state.dialogVisible;
            console.log('调用');
        },
        // 点击登录
        submit_login(state, arrdata) {
            let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            let pass = /^[a-zA-Z0-9]\w{8,17}$/;

            // 判断手机号码和密码是否正确
            if (!reg.test(arrdata.phone) || !pass.test(arrdata.password)) {
                // console.log('请输入正确手机号码与密码');
                ElMessage.error({
                    message: '请输入正确手机号码与密码',
                });
                return;
            } else {

                ElMessage.success({
                    message: '登录成功',
                });

                // // 登录后显示追漫、历史和头像
                state.loginShow = !state.loginShow;

                // // 关闭登录框
                state.dialogVisible = !state.dialogVisible;
                // console.log('登录成功');


                // 登录成功后将token值上传
                state.token = [
                    {
                        'name': '时夜',
                        'imgurl': 'https://i1.hdslb.com/bfs/face/67a50a3cd7a4d50996c3f89d2e6c34edf2163839.jpg@150w_150h.jpg',
                        'loginShow': state.loginShow
                    }
                ];

                // 上传token值
                window.localStorage.setItem("token", JSON.stringify(state.token));

                window.location.reload();
                return;
            }

        },
        EnterFun(state, arr) {
            // console.log(arr);
            if (arr[0] == 'historyFlag') {
                state.historyFlag = !arr[1];
                // console.log(state.historyFlag);
            } else if (arr[0] == 'cartoonFlag') {
                state.cartoonFlag = !arr[1];
                // console.log(state.cartoonFlag);
            } else if (arr[0] == 'userFlag') {
                state.userFlag = !arr[1];
                console.log(state.userFlag);
            }
        },
        LeaveFun(state, arr) {
            if (arr[0] == 'historyFlag') {
                state.historyFlag = !arr[1];
            } else if (arr[0] == 'cartoonFlag') {
                state.cartoonFlag = !arr[1];
            } else if (arr[0] == 'userFlag') {
                state.userFlag = !arr[1];
            }
        },
        submit_comment(state, arr) {
            let data = (new Date().getTime()) / 1000;
            // console.log('评论数据', arr.comment);
            // console.log(arr.comment.every(item => item.oid == arr.id));
            if (arr.comment.every(item => item.oid == arr.id)) {
                arr.comment.unshift({
                    'oid': arr.id,
                    "member": {
                        "mid": "6910931",
                        "uname": "哀",
                        "sex": "男",
                        "sign": "",
                        "avatar": "https://i0.hdslb.com/bfs/face/52787b7accb69a3c87e80a1bca9bcc0ca5f3e06b.jpg",
                    },
                    "content": {
                        "message": arr.value,
                    },
                    "ctime": data,
                })
                localStorage.setItem('comment', JSON.stringify(arr.comment));
            }
        }
    },

    actions: {
    },

    modules: {
    }
};

export default ModuleLogin;
